<?php

namespace app\home\service;

use app\home\model\Player;
use app\home\model\TradeLog;
use app\tools\BillingPoint;

class OrderService extends TradeLog
{

    const PLAYER_NOT_FOUND = 1;
    const REPEAT_TRADE_AT_TIME = 2;

    public function createOrder($params)
    {
        $UserID = $params['userid'];
        $GoodsID = $params['stroeid'];
        $Channel = $params['channel'];

        try {
            $player = new Player();
            $Account = $player->where("UserID",$UserID)->find();
            if (!$Account) {
                $this->error = '用户找不到';
                return false;
            }

            if (self::isExistsAtSameTimeByUserId($UserID)) {
                $this->error = '同一秒订单重复';
                return false;
            }
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }


        // 充值限额处理
        $limitAmount = new LimitAmount($UserID);
        $isCanRecharge = $limitAmount->checkIsCanRecharge();
        if ($isCanRecharge != 1) {
            $this->error = '出现限额 '.$isCanRecharge;
            return false;
        }

        // 限购计费点 一个玩家只能买一次
        if (self::Check_Forever_Limited_Purchase($UserID, $GoodsID)) {
            $this->error = '你已经购买过这个礼包啦，不要太贪心哦！';
            return false;
        }

        $BillingPoint = BillingPoint::getBillingPointById($GoodsID);
        if($BillingPoint === false){
            $this->error = '计费点找不到！';
            return false;
        }

        $OrderId = get_microtimes();
        $data = [
            'id' => $OrderId,
            'userid' => $UserID,
            'goods_id' => $GoodsID,
            'goods_name' =>$BillingPoint['Title'],
            'amount' => $BillingPoint['Price'],
            // 'usd' => $BillingPoint['Usd'],
            'status' => 0, //默认是0,表示未付款
            'channel_id' => $Channel,
            'SpreaderID' => $Account['SpreaderID'],
            'createdat' => date('Y-m-d H:i:s'),
            'registerat' => $Account['RegisterDate'],
            'loginIp' => get_client_ip()
        ];
        $res = self::create($data);
        if ($res == false) {
            $this->error = '创建订单失败';
            return false;
        }

        return $data;
    }

    /**
     * 一个玩家只能买一次的计费点
     * @param $user_id
     * @param $Billing_point
     * @return bool
     * @throws \think\Exception
     */
    public function Check_Forever_Limited_Purchase($user_id, $Billing_point)
    {
        $arr = config('CHECK_FOREVER_LIMITED_PURCHASE_BAGS');
        if (in_array($Billing_point, $arr)) {
            $map = [];
            $map['userid'] = $user_id;
            $map['goods_id'] = $Billing_point;
            $map['status'] = 1;
            $count = self::where($map)
                ->count();
            if ($count >= 1) {
                return true;
            }
        }

        return false;
    }

    public function isExistsAtSameTimeByUserId($userid){
        $time = date("Y-m-d H:i:s");
        if(self::where('userid',$userid)->where('createdat',$time)->find()){
            return true;
        }

        return false;
    }
}